define(['template','jquery','cookie'],(template) => {
   class Header{
      constructor(url){
         // this.loadHTML()
         this.url = url
         //cookie name
         this.name = $.cookie('name')
         console.log(this.name)
         this.loadHTML().then(() => {
            //这儿调用方法

            this.getMsg().then(()=>{
               this.click()
               this.calcCartCount()
               this.removeNav()
               this.cookieA()
               this.search()

            })
         })
      }
      //全部都要
      loadHTML (){
         return new Promise(resolve => {
            //承诺兑现后 再执行
            
            $('header').load('/html/modules/header.html',resolve)
            
         })
         // $('header').load('/html/modules/header.html',()=>{
         // resolve()
         // })
      }
      getMsg(){
         return new Promise(resolve => {
            $.get('/v1/cms/second_nav',resp=>{
               // console.log(resp)
               $('#bottom').html(template('bottomTemplate',{
                  list:resp
               }))
               resolve()
            })
         })
         
      }
      click(){
         // console.log(location)
         //添加搜素框
         
         $('#bottom').append(`
         <li class="search" style='width:330px;'>
            
         </li>
         `)
        
         // $('#bottom').children().children().eq(0).addClass('click')
         // $('#bottom').children().eq(0).addClass('click')
         // console.log($('#bottom').children())
         const id = location.search.slice(4)
         $('.menu').each(function(index,item){
            if($(this).data('id') == id){
               $(this).addClass('click')
            }
         })
      }
      //获取购物车数量
      calcCartCount(){
         let count = 0
         let cart = localStorage.getItem(`cart${this.name}`)
         if(cart){
            cart = JSON.parse(cart)
            count = cart.reduce((num,shop)=>{
               return num+shop.count
            },0)
         }
         $('#header-num').html(count)
         $('#shopNum-num').html(count)
      }
      //清除nav
      removeNav(){
         
         if((location.href).indexOf('buyShop') > 0 || (location.href).indexOf('login') > 0){
         $('.header-bottom').remove()
         }
      }
      //判断是否有cookie
      cookieA(){
         $('.username-div').hide()
         let nameCookie = $.cookie('name')
         if(nameCookie){
            $('#name').html(nameCookie)
            $('.username').css('background-image','url("/images/header/havename.png")')
            $('.usernameBox').on('mouseenter',()=>{
               $('.username-div').show()
            })
            $('.usernameBox').on('mouseleave',()=>{
               $('.username-div').hide()
            })
         }else{
            $('.username-div').hide()
            $('.username').css('background-image','url("/images/header/username.png")')
         }
         $('#username-div-quit').on('click',()=>{
            $.removeCookie('name',{path:'/'})
            $('.username').css('background-image','url("/images/header/username.png")')
            // $('.username-div').hide()
            alert('退出登录成功')
            $('.usernameBox').off('mouseenter')
            location.href='/index.html'
         })
      }
      //search
      search(){
         $('.search-btn').on('click',function(){
            this.word = $(this).parent().prev().val()
            // console.log(word)
            $(this).parent().attr('href',`/html/search.html?word=${this.word}`)

         })
      }
   }
   return  new Header()
})